package com.example.myapplication.found.model;

import com.example.myapplication.found.bean.KDJBean;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.StockMinPointBean;
import com.example.myapplication.found.bean.StockOrderBean;
import com.example.myapplication.found.contract.PointCallback;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: rongfeng
 * @date: 2022/2/11
 * @description
 */
public class Point {
    public static boolean isTest = true;
    private String fundCode;
    private String fundName;
    private StockBean stockBean;
    private StockBean weekStockBean;
    private ExecutorService pool;
    int endDay = 1;
    public int interval = 63;//时间间隔梯度
    public float differ = 3f;
    int differDay = 10;
    PointCallback simpleCallback;
    List<StockOrderBean> orderList = new ArrayList<>();

    KDJ kdj;

    public void setSimpleCallback(PointCallback simpleCallback) {
        this.simpleCallback = simpleCallback;

    }

    public Point(StockBean stockBean, StockBean weekStockBean) {
        this.fundCode = stockBean.code;
        this.fundName = stockBean.name;
        this.stockBean = stockBean;
        this.weekStockBean = weekStockBean;
        init();
    }

    public void init() {
        pool = Executors.newFixedThreadPool(1);
        kdj = new KDJ(stockBean, weekStockBean);
        kdj.initKDJ();
    }

    List<Float> avglist = new ArrayList<>();

    public void start() {
        pool.execute(new Runnable() {
            @Override
            public synchronized void run() {
                if (endDay < stockBean.klineList.size() - 1) {
                    endDay++;
                    test1();
                } else {
                    float orderZhang = 0, orderDie = 0, orderPin = 0, orderAvg = 0, money = 0;
                    for (StockOrderBean orderBean : orderList) {
                        if (orderBean.zhang) {
                            orderZhang++;
                        }
                        if (orderBean.die) {
                            orderDie++;
                        }
                        if (orderBean.pin) {
                            orderPin++;
                        }
                        money = money + orderBean.money;
                        orderAvg = orderAvg + orderBean.average;
                    }
                    if (orderList.size() > 0) {
                        System.out.println(fundName + " " + fundCode);
                        System.out.println("买入涨 " + orderZhang + " 跌 " + orderDie + "  平 " + orderPin + " 胜率 " + (orderZhang / orderList.size()) * 100 + "%");
                        System.out.println("平均剩余金额 " + money / orderList.size());
                        System.out.println();
                    }
                    if (simpleCallback != null) {
                        simpleCallback.pointCallback(orderList);
                    }
                }
            }
        });
    }


    //找最低点
    List<StockMinPointBean> getMinPointList() {
        List<StockMinPointBean> minPointBeanList = new ArrayList<>();
        int index2 = 0;
        for (int i = endDay; i >= 0 && i > endDay - 180; i--) {
            StockKlineBean item = stockBean.klineList.get(i);
            int stepDay = (index2 / interval) + 1;
            index2++;
            if (minPointBeanList.size() < stepDay) {
                minPointBeanList.add(new StockMinPointBean(stepDay * interval, item));
                i = endDay;
                index2 = 0;
            } else {
                for (int j = 0; j < minPointBeanList.size(); j++) {
                    StockMinPointBean minPointBean = minPointBeanList.get(j);
                    if (item.index > endDay - minPointBean.day) {
                        if (item.shouPan < minPointBean.dayPointBean.shouPan) {
                            minPointBean.dayPointBean = item;
                            i = endDay;
                            index2 = 0;
                        }
                    }
                }
            }
        }
        if (isTest) {
            for (StockMinPointBean minPointBean : minPointBeanList) {
//                System.out.println("第" + endDay + " 轮 " + (minPointBean.day / interval) + "阶段最低点 " + minPointBean.dayPointBean.shouPan + " " + minPointBean.dayPointBean.date);
            }
        }
        return minPointBeanList;
    }

    //最低点测试数据
    void test1() {
        float money = 10000;
        List<StockMinPointBean> minPointBeanList = getMinPointList();
        StockKlineBean item = stockBean.klineList.get(endDay);
        //各个区间测试
        for (StockMinPointBean minPointBean : minPointBeanList) {
            if (item.index > minPointBean.dayPointBean.index + 63 && item.index - minPointBean.dayPointBean.index < 180) {
                float cValue = (item.shouPan - minPointBean.dayPointBean.shouPan) / minPointBean.dayPointBean.shouPan * 100f;
//               && item.chg>0   && kdj.isKdjPoint(item.date)
                if (Math.abs(cValue) <= differ && item.chg > 0) {
                    if (item.index == stockBean.klineList.size() - 1 || isTest) {
                        if (!isTest) {
                            System.out.println(fundName + " " + fundCode);
                            System.out.println("找到买点 " + item.date + " 支撑日期 " + minPointBean.dayPointBean.date + " 时间间隔 " + (item.index - minPointBean.dayPointBean.index) + " 天");
                        }
                        System.out.println("找到买点 " + item.date + " 支撑日期 " + minPointBean.dayPointBean.date + " 时间间隔 " + (item.index - minPointBean.dayPointBean.index) + " 天");
                        DecimalFormat decimalFormat = new DecimalFormat("0.0000");//构造方法的字符格式这里如果小数不足2位,会以0补足.
                        String p2 = decimalFormat.format(cValue);
                        System.out.println("点位误差 " + p2 + "%");
                    }
                    StockKlineBean endDayBean = null;
                    for (int j = 1; j <= differDay; j++) {
                        if (endDay + j < stockBean.klineList.size()) {
                            StockKlineBean nextItem = stockBean.klineList.get(j + endDay);
                            money = money + (money * (nextItem.chg / 100));
                            endDayBean = nextItem;
                            if (isTest) {
                                System.out.println(nextItem.date + " " + nextItem.chg + "%");
                            }
                        }
                    }
                    float average = 0;
                    if (endDayBean != null) {
                        average = (endDayBean.shouPan - item.shouPan) / item.shouPan * 100f;
                    }

                    avglist.add(average);

                    StockOrderBean orderBean = new StockOrderBean();
                    orderBean.stockKlineBean = item;
                    orderBean.average = average;
                    orderBean.zhang = average > 0;
                    orderBean.die = average < 0;
                    orderBean.pin = average == 0;
                    orderBean.money = money;
                    orderList.add(orderBean);

                    endDay = endDay + differDay;
                    if (endDay > stockBean.klineList.size() - 1) {
                        endDay = stockBean.klineList.size() - 1;
                    }
                    if (isTest) {
                        System.out.println((differDay) + "天涨幅 " + average + "%");
                        System.out.println("剩余金额 " + money);
                        System.out.println();
                    }
                    break;
                }
            }
        }
        start();
    }
}
